<template>
  <div>
    <el-form :model="readerQueryForm" inline ref="readerQueryRef">
      <el-form-item prop="keyword">
        <el-input v-model="readerQueryForm.keyword" placeholder="请输入关键字"></el-input>
      </el-form-item>
      <el-form-item prop="readerTypeId">
        <el-select v-model="readerQueryForm.typeId" placeholder="请选择类型" style="width: 240px">
          <el-option :label="item.typeName" :value="item.typeId" v-for="item in readerTypes"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getReaderList">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="readerQueryRef.resetFields()" icon="Refresh">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="small" @click="dialogVisible = true" icon="Plus" v-permission="'reader:create'">添加</el-button>
    <excel-export-button url="/reader/export" title="读者列表"></excel-export-button>
    <excel-import-button  url="/reader/import"></excel-import-button>
    <el-table :data="readerPage.list" v-loading="loading">
      <el-table-column type="expand">
        <template #default="props">
          <div m="4">
            <p m="t-0 b-2">电子邮箱: {{ props.row.email }}</p>
            <p m="t-0 b-2">联系电话: {{ props.row.phone }}</p>
            <p m="t-0 b-2">身份证号: {{ props.row.idCard }}</p>
            <p m="t-0 b-2">联系地址: {{ props.row.address }}</p>
            <p m="t-0 b-2">创建时间: {{ props.row.createdTime }}</p>
            <p m="t-0 b-2">更新时间: {{ props.row.updatedTime }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="readerId" label="读者ID"></el-table-column>
      <el-table-column prop="readerNo" label="读者证编号"></el-table-column>
      <el-table-column prop="typeId" label="读者类型ID"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="gender" label="性别">
        <template #default="scoped">
          <el-text v-if="scoped.row.gender === 0">男</el-text>
          <el-text v-if="scoped.row.gender === 1">女</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="birthDate" label="出生日期">
        <template #default="scoped">
          {{ $timeFormat(scoped.row.birthDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="registerDate" label="注册日期">
        <template #default="scoped">
          {{ $timeFormat(scoped.row.registerDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="scoped">
          <el-text v-if="scoped.row.status === 0">正常</el-text>
          <el-text v-if="scoped.row.status === 1">禁用</el-text>
          <el-text v-if="scoped.row.status === 2">注销</el-text>
          <el-text v-if="scoped.row.status === 3">黑名单</el-text>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="190px">
        <template #default="scoped">
          <el-button type="warning" size="small" @click="handleEdit(scoped.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scoped.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <div class="flex-end">
      <el-pagination
          background
          layout="prev, pager, next"
          :total="readerPage.total"
          v-model:page-size="readerQueryForm.pageSize"
          v-model:current-page="readerQueryForm.pageNum"
      >
      </el-pagination>
    </div>
    <el-dialog
        v-model="dialogVisible"
        :title="isAdd ? '新增读者':'修改读者'"
        width="800"
        draggable
        @close="closeHandel"
    >
      <el-form
          :model="readerSaveForm"
          :rules="rules"
          ref="readerSaveFormRef"
          label-width="100px"
          inline
      >
        <el-form-item label="读者证编号" prop="readerNo">
          <el-input v-model="readerSaveForm.readerNo" placeholder="请输入读者证编号" clearable/>
        </el-form-item>
        <el-form-item label="读者类型" prop="typeId">
          <el-select v-model="readerSaveForm.typeId" placeholder="请选择读者类型" clearable style="width: 240px">
            <el-option
                v-for="item in readerTypes"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="readerSaveForm.name" placeholder="请输入姓名" clearable/>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="readerSaveForm.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
            <el-radio :label="3">其他</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker
              v-model="readerSaveForm.birthDate"
              type="date"
              placeholder="选择出生日期"
              value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input
              v-model="readerSaveForm.idCard"
              placeholder="请输入18位身份证号"
              maxlength="18"
              show-word-limit
              clearable
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="readerSaveForm.phone" placeholder="请输入联系电话" clearable/>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="readerSaveForm.email" placeholder="请输入电子邮箱" clearable/>
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input
              v-model="readerSaveForm.address"
              type="textarea"
              :rows="2"
              placeholder="请输入详细地址"
          />
        </el-form-item>
        <el-form-item label="注册日期" prop="registerDate">
          <el-date-picker
              v-model="readerSaveForm.registerDate"
              type="date"
              placeholder="选择注册日期"
              value-format="YYYY-MM-DD"
              disabled
          />
        </el-form-item>
        <el-form-item label="证件有效期" prop="expireDate">
          <el-date-picker
              v-model="readerSaveForm.expireDate"
              type="date"
              placeholder="选择有效期"
              value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="readerSaveForm.status" placeholder="请选择状态" style="width: 240px">
            <el-option label="正常" :value="0"/>
            <el-option label="挂失" :value="1"/>
            <el-option label="注销" :value="2"/>
            <el-option label="黑名单" :value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息" prop="memo">
          <el-input
              v-model="readerSaveForm.memo"
              type="textarea"
              :rows="3"
              placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitSave(saveReaderFormRef)">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref, watch} from "vue";
import {reqDeleteReader, reqGetReaderPage, reqSaveReader, reqUpdateReader} from "@/api/reader";
import {ElMessage, ElMessageBox} from "element-plus";
import {reqGetReaderTypeList} from "@/api/reader/readerType.js";

let readerQueryRef = ref()
let dialogVisible = ref(false)
let saveReaderFormRef = ref(null)
let readerSaveForm = reactive({
  readerId: null,
  readerNo: '',
  typeId: null,
  name: '',
  gender: 1,
  birthDate: '',
  idCard: '',
  phone: '',
  email: '',
  address: '',
  registerDate: new Date(), // 默认当天
  expireDate: '',
  status: 0, // 默认正常
  memo: ''
})
let readerTypes = ref([])
let loading = ref(true)
let isAdd = ref(true)
let readerQueryForm = reactive({
  pageNum: 1,
  pageSize: 15,
  keyword: '',
  typeId: '',
})
let readerPage = reactive({
  list: [],
  total: 0,
  pageNUm: 1,
})

let resetReaderForm = () => {
  readerSaveForm.readerNo = ''
  readerSaveForm.typeId = null
  readerSaveForm.name = ''
  readerSaveForm.gender = 1
  readerSaveForm.birthDate = ''
  readerSaveForm.idCard = ''
  readerSaveForm.phone = ''
  readerSaveForm.email = ''
  readerSaveForm.address = ''
  readerSaveForm.registerDate = new Date()
  readerSaveForm.expireDate = ''
  readerSaveForm.status = 0
}

let getReaderTypes = async () => {
  let res = await reqGetReaderTypeList()
  readerTypes.value = res.data
}
let closeHandel = () => {
  isAdd.value = true
  resetReaderForm()
}
let getReaderList = async () => {
  let res = await reqGetReaderPage(readerQueryForm)
  Object.assign(readerPage, res.data)
  loading.value = false
}
let handleEdit = (node) => {
  isAdd.value = false
  Object.assign(readerSaveForm, node)
  dialogVisible.value = true
}

let submitSave = (formEl) => {
  formEl.validate(async valid => {
    if (valid) {
      if (isAdd.value) {
        await reqSaveReader(readerSaveForm)
        await getReaderList()
      } else {
        await reqUpdateReader(readerSaveForm)
        await getReaderList()
      }
    }
  })
}
let handleDelete = (reader) => {
  ElMessageBox.confirm(
      `确定要删除读者:【${reader.name}】?`,
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        reqDeleteReader(reader.readerId)
            .then(() => {
              getReaderList()
            })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: 'Delete canceled',
        })
      })
}

const rules = reactive({
  readerNo: [
    {required: true, message: '请输入读者证编号', trigger: 'blur'}
  ],
  typeId: [
    {required: true, message: '请选择读者类型', trigger: 'change'}
  ],
  name: [
    {required: true, message: '请输入姓名', trigger: 'blur'},
    {min: 2, max: 20, message: '长度在2到20个字符', trigger: 'blur'}
  ],
  idCard: [
    {required: true, message: '请输入身份证号', trigger: 'blur'},
    {pattern: /^\d{17}[\dXx]$/, message: '请输入正确的身份证号', trigger: 'blur'}
  ],
  phone: [
    {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'}
  ],
  email: [
    {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
  ],
  expireDate: [
    {required: true, message: '请选择证件有效期', trigger: 'change'}
  ]
})
onMounted(() => {
  getReaderList()
  getReaderTypes()
})
watch([() => readerQueryForm.pageNum, () => readerQueryForm.pageSize], () => {
  getReaderList()
})
</script>

<style scoped>

</style>
